Imports VB = Microsoft.VisualBasic
Friend Class frmProgress
    Inherits System.Windows.Forms.Form

    'dimensions and constants

    Dim NumRecords As Integer
    Dim LastLine As String
    Dim SomeData As Boolean
    Dim t(10) As Object
    Dim LastTime As Single
    Dim outfile As String
    Public StartTime As Single
    Dim Abort As Boolean
    'Dim FastFile As clsFastFile
    Public ImportCancelled As Boolean

    Public WRDBImport As clsWRDBImport

    Private Sub btnContinue_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles btnContinue.Click
        If btnContinue.Text = "Close" Then 'must have cancelled during processing
            DialogResult = Windows.Forms.DialogResult.Cancel
        Else 'caption is "Continue"---write msg file and tell WRDB to continue
            FileClose()
            DialogResult = Windows.Forms.DialogResult.OK
        End If
        SaveSetting(RegApp, "General", "AUTOCONTINUE", IIf(chkAutoContinue.CheckState, "TRUE", "FALSE"))
        Hide()
    End Sub

    Private Sub frmProgress_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
        chkAutoContinue.Checked = WRDBImport.AutoContinue
        Gauge.Value = 0
        chkAutoContinue.Visible = True
        'this is a modeless dialog; make it look modal...
        Activate()
    End Sub

    Private Sub frmProgress_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        SaveSetting(RegApp, "General", "AUTOCONTINUE", CStr(chkAutoContinue.CheckState = System.Windows.Forms.CheckState.Checked))
    End Sub

    Private Sub btnCopy_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles btnCopy.Click
        Clipboard.SetText(Me.TextBox.Text.Replace(vbLf, vbCrLf), TextDataFormat.Text)
    End Sub

    Private Sub btnCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles btnCancel.Click
        If MessageBox.Show("Are you sure you want to cancel the import?", "WRDB Import", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then Exit Sub
        Me.Cursor = System.Windows.Forms.Cursors.Default
        With btnContinue
            .Text = "Close"
            .Enabled = True
            VB6.SetDefault(btnContinue, True)
            VB6.SetCancel(btnContinue, True)
        End With
        btnCancel.Enabled = False
        btnCopy.Enabled = True
        ImportCancelled = True
        chkAutoContinue.Checked = False
        btnContinue.PerformClick()
        FileClose()
        DialogResult = Windows.Forms.DialogResult.Cancel
        Hide()
    End Sub

    Friend Sub UpdateProgress(ByVal Progress As Double)

        Dim ImportRate As Double 'percent per second
        Dim minremain, secremain As Integer
        Static LastProgress As Single

        'update progress bar

        Gauge.Value = CShort(Progress)
        ImportRate = (Progress) / (VB.Timer() - StartTime)
        If ImportRate = 0 Then Exit Sub
        secremain = (100 - Progress) / ImportRate
        minremain = Int(secremain / 60.0#)
        secremain = secremain - minremain * 60

        Frame1.Text = String.Format("Percent Complete (Time Remaining: {0}:{1:00})", minremain, secremain)

        LastTime = VB.Timer()
        LastProgress = Progress
        Application.DoEvents()

    End Sub

    Friend Sub DoImport()
        Dim t1 As Single

        ImportCancelled = False

        System.Windows.Forms.Application.DoEvents()

        Frame1.Text = "Percent Complete"
        Beep()
        Gauge.Value = Gauge.Maximum
        btnContinue.Enabled = True
        VB6.SetDefault(btnContinue, True)
        btnContinue.Text = "Continue"
        btnContinue.Focus()
        btnCancel.Enabled = True
        btnCopy.Enabled = True

        If chkAutoContinue.Checked Then 'enter loop that will cause form to close after specified time
            t1 = VB.Timer()
            Do While VB.Timer() - t1 < WRDBImport.AutoContinueDelay And chkAutoContinue.Checked And DialogResult = Windows.Forms.DialogResult.None
                Threading.Thread.Sleep(100)
                System.Windows.Forms.Application.DoEvents()
                Frame1.Text = String.Format("Time Until Auto-Continue: {0:0} sec.", WRDBImport.AutoContinueDelay - (VB.Timer() - t1))
                If ImportCancelled Then Exit Do
            Loop
            btnCancel.Enabled = False
            btnContinue.PerformClick()
        Else 'since this is a modeless dialog, enter infinite loop waiting for user to press a button
            Do
                Threading.Thread.Sleep(100)
                Application.DoEvents()
            Loop Until DialogResult <> Windows.Forms.DialogResult.None
        End If
    End Sub

End Class